import {getUserInfo} from '@/api/user'
import { Dialog,Toast } from 'vant'
import router from '@/router'


const state = {
  //token
  authorization:localStorage.getItem('authorization') ? localStorage.getItem('authorization'): '',
  user:null
}



const actions = {
  outlogin({state,commit}){
    commit('OUTLOGIN');
  },
  login({state,commit},payload){
    commit('LOGIN',payload);
  },
  UserInfo({state}){
    if(state.user){
      return
    }
    getUserInfo().then(data=>{
      if(data.resultCode === 200){
        state.user = data.data;
      }else{
        state.user =null;
      }
    }).catch(err=>{
      console.log(err);
    })
  }
}

const mutations={
  OUTLOGIN(state){
    state.user = null;
    state.authorization='';
    localStorage.removeItem('authorization');
  },
  LOGIN(state,payload){
    state.user = payload.data;
    state.authorization = payload.token;
  },
  // 修改token，并将token存入localStorage
  changeLogin (state, user) {
    state.Authorization = user.authorization;
    localStorage.setItem('authorization', user.authorization);
  }
}

export default{
  namespaced: true,
  state,
  mutations,
  actions
}